/*
 * $Id: GarbageFreeDoubleFormatter.java 66 2013-01-06 10:16:51Z vladimirow@mail.ru $
 * $HeadURL: http://ultralog.googlecode.com/svn/ultralog/tags/release-20130123/src/java/com/google/code/ultralog/formatter/gfree/GarbageFreeDoubleFormatter.java $
 *
 * This file is a part of ultralog project (http://code.google.com/p/ultralog/).
 * Released under New BSD License.
 */
package com.google.code.ultralog.formatter.gfree;

import com.google.code.ultralog.formatter.DoubleFormatter;
import com.google.code.ultralog.utils.CharacterOutputStream;

/**
 * Garbage free implementation of {@link DoubleFormatter} interface.
 *
 * @author Mikhail Vladimirov
 */
public class GarbageFreeDoubleFormatter implements DoubleFormatter
{
    /**
     * Singleton instance of the formatter.
     */
    public final static GarbageFreeDoubleFormatter INSTANCE =
        new GarbageFreeDoubleFormatter ();

    private GarbageFreeDoubleFormatter ()
    {
        // Do nothing
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void format (double value, CharacterOutputStream output)
    {
        if (output == null)
            throw new IllegalArgumentException ("Output is null");

        double m = Math.abs (value);

        if (m > 1.0e-3 && m < 1.0e7)
            GarbageFreeFormatterUtils.writeDouble (value, output);
        else
            GarbageFreeFormatterUtils.writeDoubleScientific (value, output);
    }
}
